import React, { useEffect, useState } from 'react';
import styles from './index.css';
import { Form, Input, Button, Divider, Radio } from 'antd';
import 'antd/dist/antd.css';
import moment from 'moment';
import { seckill } from './service'
let timer: any = null
const Demo = () => {
  const [time, setTime] = useState('-时-分-秒-毫秒')
  const [result, setresult] = useState('')
  const startTime = '2022-3-14 11:35:00' // 开始秒杀的时间
  const [params, setparams] = useState(
    {
      tk: "wxapptoken:10:a6bbbfabe7af225ed936d3d9aee27349_48019fec378dc0c7b1401017dd51e29b", //微信token
      seckillId: 1665, //秒杀场次
      linkmanId: 22790778, //抢的身份id
      idCardNo: 510811199808212940 //约苗身份证号码
    }
  )
  const request = () => {
    let exprie = moment(startTime).valueOf() - moment().valueOf()
    let h = moment.duration(exprie)._data.hours
    let m = moment.duration(exprie)._data.minutes
    let s = moment.duration(exprie)._data.seconds
    let ms = moment.duration(exprie)._data.milliseconds
    if (exprie <= 50) {
      clearInterval(timer)
      seckill(params).then(res => {
        setTimeout(() => {
          seckill(params).then(res => {
            setresult(JSON.stringify(res))
          })
        }, 25);
        setresult(JSON.stringify(res))
      })
    }
    if (exprie > 0) {
      setTime(`${h}时${m}分${s}秒${ms}毫秒`)
    } else {
      setTime(`0毫秒`)
    }
  }
  return (
    <div>
      <ul className={styles.list}>
        <Button onClick={() => {
          clearInterval(timer)
          timer = setInterval(() => {
            request()
          }, 10)
        }}>开始秒杀</Button>
        <h2 style={{ lineHeight: 2 }}>
          还有{time}
        </h2>
        <Button onClick={() => {
          clearInterval(timer)
        }}>结束秒杀</Button>
        <div>
          <h2>秒杀结果</h2>
          <div>{result}</div>
        </div>
      </ul>
    </div>

  );
}
export default Demo
